using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TinyERP.Framework;
using TinyERP.Framework.Controls;
using Bamboo.Core.Data;
using Bamboo.Core.Common.Collection;
using Bamboo.Core.Common.Logger;
using TinyERP.Core.Business;
using TinyERP.Core.DTO;

namespace TinyERP.Core.View
{
    public class UserListForm : BaseListForm
    {
		private DataGridViewTextBoxColumn userNameColumn;
		private DataGridViewTextBoxColumn passwordColumn;
		private DataGridViewTextBoxColumn fullNameColumn;
		private DataGridViewTextBoxColumn emailColumn;
		private DataGridViewTextBoxColumn birthdayColumn;
		private DataGridViewTextBoxColumn userGroupColumn;

		public IUserService UserService 
		{
			get
			{
				return (IUserService) ServiceLocator.GetService("UserServiceProxy");
			}
		}
		

        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
			this.SuspendLayout();
			this.userNameColumn = new DataGridViewTextBoxColumn() ;
			this.passwordColumn = new DataGridViewTextBoxColumn() ;
			this.fullNameColumn = new DataGridViewTextBoxColumn() ;
			this.emailColumn = new DataGridViewTextBoxColumn() ;
			this.birthdayColumn = new DataGridViewTextBoxColumn() ;
			this.userGroupColumn = new DataGridViewTextBoxColumn();
			this.ResumeLayout(false);

        }

        #endregion

        
        
        private void SetupDataGridView()
        {
this.dataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
			this.userNameColumn
			,
			this.passwordColumn
			,
			this.fullNameColumn
			,
			this.emailColumn
			,
			this.birthdayColumn
			,
			this.userGroupColumn
            });
            
			this.userNameColumn.DataPropertyName = "UserName";
			this.userNameColumn.HeaderText = ModuleResourceManager.GetInstance().GetString("UserName", "TinyERP.Core");
			this.userNameColumn.Name = "userNameColumn";
			this.userNameColumn.Tag = "userName";
			this.userNameColumn.ReadOnly = true;
			this.userNameColumn.SortMode = DataGridViewColumnSortMode.Programmatic;
			this.passwordColumn.DataPropertyName = "Password";
			this.passwordColumn.HeaderText = ModuleResourceManager.GetInstance().GetString("Password", "TinyERP.Core");
			this.passwordColumn.Name = "passwordColumn";
			this.passwordColumn.Tag = "password";
			this.passwordColumn.ReadOnly = true;
			this.passwordColumn.SortMode = DataGridViewColumnSortMode.Programmatic;
			this.fullNameColumn.DataPropertyName = "FullName";
			this.fullNameColumn.HeaderText = ModuleResourceManager.GetInstance().GetString("FullName", "TinyERP.Core");
			this.fullNameColumn.Name = "fullNameColumn";
			this.fullNameColumn.Tag = "fullName";
			this.fullNameColumn.ReadOnly = true;
			this.fullNameColumn.SortMode = DataGridViewColumnSortMode.Programmatic;
			this.emailColumn.DataPropertyName = "Email";
			this.emailColumn.HeaderText = ModuleResourceManager.GetInstance().GetString("Email", "TinyERP.Core");
			this.emailColumn.Name = "emailColumn";
			this.emailColumn.Tag = "email";
			this.emailColumn.ReadOnly = true;
			this.emailColumn.SortMode = DataGridViewColumnSortMode.Programmatic;
			this.birthdayColumn.DataPropertyName = "Birthday";
			this.birthdayColumn.HeaderText = ModuleResourceManager.GetInstance().GetString("Birthday", "TinyERP.Core");
			this.birthdayColumn.Name = "birthdayColumn";
			this.birthdayColumn.Tag = "birthday";
			this.birthdayColumn.ReadOnly = true;
			this.birthdayColumn.SortMode = DataGridViewColumnSortMode.Programmatic;
			this.userGroupColumn.DataPropertyName = "UserGroupName";
			this.userGroupColumn.HeaderText = ModuleResourceManager.GetInstance().GetString("UserGroup", "TinyERP.Core");
			this.userGroupColumn.Name = "userGroupColumn";
			this.userGroupColumn.Tag = "userGroup";
			this.userGroupColumn.ReadOnly = true;
			this.userGroupColumn.SortMode = DataGridViewColumnSortMode.NotSortable;

            this.dataGridView.AutoGenerateColumns = false;
            this.dataGridView.MultiSelect = false;
            this.dataGridView.ResourceModuleKey = "TinyERP.Core";
            this.dataGridView.AllowUserToOrderColumns = true;
            this.dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            InitializeSortKeyMap();
        }
        
        private void InitializeSortKeyMap() 
        {
			this.dataGridView.SortKeyMap["UserGroupName"] = "UserGroup.Name";
        }

		public UserListForm() : base()
        {
            InitializeComponent();
			SetupDataGridView();
        }
        

		public override void OnNew()
        {
            UserEditInfoForm editForm = new UserEditInfoForm();
            editForm.OnNeedReloadData += delegate(object senderObj)
            {
                LoadGridData();
            }; 
            DialogDecorator dialog = new DialogDecorator(editForm);
            dialog.ShowDialog();
        }
        

		public override void OnDelete()
        {
			if (this.dataGridView.SelectedRows.Count == 0)
            {
                return;
            }
            UserDTO user = (UserDTO)this.dataGridView.SelectedRows[0].DataBoundItem;
            try 
			{
                if (MessageBox.Show(CommonMessage.GetDeleteConfirmMessage("User"),
                CommonMessage.GetConfirmationText(), MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
					UserService.DeleteUserByID(user.ID);
					MessageBox.Show(CommonMessage.GetDeleteSuccessMessage("User"));
					LoadGridData();
				}
			}
			catch(Exception ex) 
			{
				LogManager.BusinessLog.Debug(ex);
				MessageBox.Show(CommonMessage.GetCanNotDeleteError("User"));
			}
        }
        
        
        public override void OnEdit()
        {
            if (this.dataGridView.SelectedRows == null || this.dataGridView.SelectedRows.Count == 0)
            {
                return;
            }
            UserDTO userDTO = (UserDTO) this.dataGridView.SelectedRows[0].DataBoundItem;
            UserEditInfoForm editForm = new UserEditInfoForm();
            editForm.User = userDTO;
			editForm.OnNeedReloadData += delegate(object senderObj)
            {
                LoadGridData();
            };            
            DialogDecorator dialog = new DialogDecorator(editForm);
            dialog.StartPosition = FormStartPosition.CenterParent;
            dialog.ShowDialog(this);
        }
        

        

        
		protected override void LoadGridViewSearchData(IList<SearchCondition> searchConditions, Bamboo.Core.Data.SortDirection sortDirection, string columnName)
        {
			long totalRecord = 0;
            IList userList = UserService.SearchUser(paging.CurrentPage, paging.PageSize, searchConditions, columnName, sortDirection, out totalRecord);
            this.paging.TotalRecord = totalRecord;
            SortableBindingList<UserDTO> sortableUserList = SortableBindingList<UserDTO>.CreateSortableBindingList(userList);
            this.dataGridView.DataSource = sortableUserList;
        }

        protected override void LoadGridViewDefaultData(Bamboo.Core.Data.SortDirection sortDirection, string columnName) 
        {
        	long totalRecord = 0;
            IList userList = UserService.GetAllUser(paging.CurrentPage, paging.PageSize, columnName, sortDirection, out totalRecord);
            this.paging.TotalRecord = totalRecord;
            SortableBindingList<UserDTO> sortableUserList = SortableBindingList<UserDTO>.CreateSortableBindingList(userList);
            this.dataGridView.DataSource = sortableUserList;
        }        

    }
}
